#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <string.h>
#include <strings.h>
#include <sys/select.h>
#include <errno.h>
#include <ctype.h>
#define OPEN_MAX 1024

void sys_err(const char* str)
{
    perror(str);
    exit(1);
}

int main(int argc,char* argv[])
{
    int sockfd,i,n,new_sockfd,ret;
    struct epoll_event temp,ep[OPEN_MAX]; 
    struct sockaddr_in server_addr,client_addr;
    char buf[BUFSIZ];
    socklen_t addr_len;
    ssize_t efd,res,count;
    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(8080);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    int opt = 1;
    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
    bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr));
    listen(sockfd,10);

    efd = epoll_create(OPEN_MAX);
    if(efd < 0)
        sys_err("epoll_create()");
    
    temp.events = EPOLLIN;
    temp.data.fd = sockfd;
    res = epoll_ctl(efd,EPOLL_CTL_ADD,sockfd,&temp);
    if(res < 0)
    {
        sys_err("epoll_ctl()");
    }

    while(1)
    {
        count = epoll_wait(efd,ep,OPEN_MAX,-1);
        if(count < 0)
            sys_err("epoll_wait()");
        for(i=0;i<count;i++)
        {
            if(!(ep[i].events & EPOLLIN))
                continue;
            if(ep[i].data.fd == sockfd)
            {
                addr_len = sizeof(client_addr);
                new_sockfd = accept(sockfd,(struct sockaddr*)&client_addr,&addr_len);
                if(new_sockfd < 0)
                    sys_err("accept()");
                
                temp.events = EPOLLIN;
                temp.data.fd = new_sockfd;
                res = epoll_ctl(efd,EPOLL_CTL_ADD,new_sockfd,&temp);
                if(res < 0)
                    sys_err("epoll_ctl()");

            }
            else
            {
                n = read(ep[i].data.fd,buf,sizeof(buf));
                if(n == 0)
                {
                    res = epoll_ctl(efd,EPOLL_CTL_DEL,ep[i].data.fd,NULL);
                    if(res < 0)
                        sys_err("epoll_ctl()");
                    close(ep[i].data.fd);
                }
                else if(n < 0)
                {
                    perror("read()");
                    res = epoll_ctl(efd,EPOLL_CTL_DEL,ep[i].data.fd,NULL);
                    if(res < 0)
                        sys_err("epoll_ctl()");
                    close(ep[i].data.fd);
                }
                else
                {
                    for(i=0;i<n;i++)
                    {
                        buf[i] = toupper(buf[i]);
                    }
                    ret = write(ep->data.fd,buf,n);
                    if(ret < 0)
                        sys_err("write()");
                    ret = write(STDOUT_FILENO,buf,n);
                    if(ret < 0)
                        sys_err("write()");
                }
            }
        }
    }
    return 0;
}
